package com.ysjy.course.controller;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.utils.StringUtil;
import com.ysjy.course.model.SignUpUser;
import com.ysjy.course.model.YsOrder;
import com.ysjy.course.service.CourseService;
import com.ysjy.course.service.OrderService;
import com.ysjy.course.service.SignUpUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(description = "报名管理")
@RequestMapping("/signUpUser/manage")
@Slf4j
public class SignUpUserManageController extends BaseController {

    @Autowired
    private SignUpUserService signUpUserService;

    @ApiOperation(value = "报名补录",notes = "{\"signUpType\":1,\"goodNumber\":\"2022052011076175\",\"mobile\":\"13613020696\",\"studentId\":3,\"amount\":1}")
    @RequestMapping(value = "putSignUpUser", method = RequestMethod.POST)
    public ResultDTO<Boolean> putSignUpUser( @Valid @RequestBody SignUpUser data){
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            if (data.getGoodNumber() == null || data.getSignUpType() == null || data.getPayType() == null || data.getStudentId() == null){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            data.setType(2);
            data.setCreateUserId(userDto.getId());
            data.setCreateUserName(userDto.getName());
            return signUpUserService.putSignUpUser(data);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
    }

    @ApiOperation(value = "报名管理")
    @RequestMapping(value = "getSignUpUserListByPage", method = RequestMethod.GET)
    public ResultDTO<PageInfo<SignUpUser>> getSignUpUserListByPage(
            @ApiParam(value = "机构ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
            @ApiParam(value = "名称（模糊搜索）", required = false) @RequestParam(name = "title", required = false) String title,
            @ApiParam(value = "学生姓名（模糊搜索）", required = false) @RequestParam(name = "stuName", required = false) String stuName,
            @ApiParam(value = "家长手机号", required = false) @RequestParam(name = "mobile", required = false) String mobile,
            @ApiParam(value = "报名方式：1小程序报名  2平台补录", required = false) @RequestParam(name = "type", required = false) Integer type,
            @ApiParam(value = "收费方式：0现金 1微信 2支付宝", required = false) @RequestParam(name = "payType", required = false) Integer payType,
            @ApiParam(value = "类型：1课程 2托管", required = false) @RequestParam(name = "signUpType", required = false) Integer signUpType,
            @ApiParam(value = "页码", required = true, defaultValue = "1") @RequestParam(name = "pageNo", required = true, defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页条数", required = true, defaultValue = "10") @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String, Object> params = new HashMap<>();
            if (!StringUtil.isEmpty(title)) params.put("title", title);
            if (!StringUtil.isEmpty(stuName)) params.put("stuName", stuName);
            if (!StringUtil.isEmpty(mobile)) params.put("mobile", mobile);
            params.put("orgId", orgId);
            params.put("type", type);
            params.put("payType", payType);
            params.put("signUpType", signUpType);
            PageInfo<SignUpUser> pageInfo = signUpUserService.getListByPage(params, pageNo, pageSize);
            return ResultDTO.successResult(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "退费")
    @RequestMapping(value = "refund", method = RequestMethod.POST)
    public ResultDTO<Boolean> refund(
            @ApiParam(value = "报名id", required = true) @RequestParam(name = "id", required = true) Long id,
            @ApiParam(value = "退款金额(分)", required = true) @RequestParam(name = "refundAmount", required = true) Integer refundAmount,
            @ApiParam(value = "退款原因", required = false) @RequestParam(name = "refundWhy", required = false) String refundWhy
    ){
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            return signUpUserService.refund(id,refundAmount,refundWhy,userDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
    }

    @ApiOperation(value = "报名详情")
    @RequestMapping(value = "getSignUpUser", method = RequestMethod.GET)
    public ResultDTO<SignUpUser> getSignUpUser(
            @ApiParam(value = "报名id", required = false) @RequestParam(name = "id", required = false) Long id) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            SignUpUser data = signUpUserService.getByPrimaryKey(id);
            return ResultDTO.successResult(data);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "删除报名记录")
    @RequestMapping(value = "delSignUpUser", method = RequestMethod.POST)
    public ResultDTO<Boolean> delSignUpUser(
            @ApiParam(value = "id", required = false) @RequestParam(name = "id", required = false) Long id) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            signUpUserService.deleteByPrimaryKey(id);
            return ResultDTO.successResult(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }


}